import { dialog } from 'electron'
import { autoUpdater } from 'electron-updater'
import http from 'http'
const path=require('path')
// autoUpdater.updateConfigPath = path.join(__filename, '../win-unpacked/resources/app-update.yml')
autoUpdater.updateConfigPath = path.join(__dirname, '../app-update.yml')
autoUpdater.on('update-downloaded', info => {
    const req = http.request(process.env.VUE_APP_HOST+'version/info.txt', req => {
        let detail = ''
        req.setEncoding('utf-8')
        req.on('data', chunk => {
        detail += chunk.toString()
        })
        req.on('end', () => {
        dialog.showMessageBox(
            {
            // icon: __static + '/favicon.png',
            type: 'info',
            title: '软件更新',
            message: `已更新到最新版本（${info.version}）请重启应用。`,
            detail: detail,
            buttons: ['确定']
            },
            idx => {
            // 点击确定的时候执行更新
            if (idx === 0) {
                autoUpdater.quitAndInstall()
            }
            }
        )
        })
    })
    req.end()
})

export default autoUpdater